<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use Illuminate\Http\Request;
use Validator;
use DB;
use Crypt;
use Auth;
use Session;

class LoginController extends AdminController
{

    /**
     * 显示所有用户。
     *
     * @param Request $request
     *
     * @return Response
     */

    protected $user;

    public function __construct()
    {
        $this->user = new Admin();
    }

//    跳转到登录页面
    public function login()
    {
        return view('login.login');
    }

    //用户登录
    public function postLogin(\Request $request)
    {
//      die;

        $request = $request::all();

        //4.验证码中取值字母都是大写,所以用户发送过来的验证码要转换下
//        if (strtoupper(Session::get('milkcaptcha')) != strtoupper($request["code"])) {
//            return back()->with('errors', array(0 => '验证码输入错误'));
//        }
//5.这个是自定义的验证，但是注意注册的时候密码一定要用"bcrypt"加密，才能被匹配到
        if (Auth::attempt(['email' => $request['email'], 'password' => ($request['password']),'type'=>'1'])) {
                return redirect('/agent');
        } else {
            return back()->with('errors', array('登录失败'))->withInput();
        }
    }

    public function captcha($tmp)
    {

        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
    }

    public function test()
    {

//开启事务
        DB::beginTransaction();
        try {
//中间逻辑代码
            Admin::where("id",5)->update(array("user"=>'4578'));
            Admin::where("name",'test')->update(array("id"=>''));
            DB::commit();
        } catch (\Exception $e) {
//接收异常处理并回滚
            DB::rollBack();
        }
    }


}
